package com.esm.pdafx.database.manager;


import com.esm.pdafx.activity.AppContext;
import com.esm.pdafx.database.dbmodel.SystemAllInfo;
import com.esm.pdafx.database.greendao.SystemAllInfoDao;
import com.esm.pdafx.model.WBox;

import java.util.ArrayList;

public class SystemCenter {

    private static SystemCenter instance = new SystemCenter();

    public static SystemCenter getInstance() {
        return instance;
    }

    public WBox wbox = new WBox();
    public String lpn = ""; //外箱码
    public int isfx = 0;
    public int num = 0; //订单总箱数
    public int wnum = 0; //已校核箱数

    //点击确定按钮后
    public void resetBarcode() {
        lpn = "";
        isfx = 0;
        wbox = new WBox();
    }

    public SystemAllInfo getSysInfo(String key) {
        if (key == null || key.length() < 1) {
            return null;
        }
        String strContent = "";
        SystemAllInfo sInfo = null;
        try {
            SystemAllInfoDao systemAllInfoDao = AppContext.getInstances().getDaoSession().getSystemAllInfoDao();
            ArrayList<SystemAllInfo> sysList = new ArrayList<SystemAllInfo>();
            sysList = (ArrayList) systemAllInfoDao.queryBuilder()
                    .where(SystemAllInfoDao.Properties.Name.eq(key))
                    .build().list();
            if (sysList.size() > 0) {
                sInfo = sysList.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return sInfo;
    }

    public Long saveSysInfo(SystemAllInfo info) {
        if (info == null) {
            return 0L;
        }

        SystemAllInfo a = getSysInfo(info.getName());
        if (a != null) {
            info.setId(a.getId());
        }

        try {
            SystemAllInfoDao systemAllInfoDao = AppContext.getInstances().getDaoSession().getSystemAllInfoDao();
            long id = systemAllInfoDao.insertOrReplace(info);
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0L;
    }

}
